#include"server.h"

int openTcpSocket(){
	
	int tcpfd;
	struct sockaddr_in serveraddr;
	int len = sizeof(serveraddr);
	if((tcpfd=socket(AF_INET,SOCK_STREAM,0))<0)
	{
		perror("fail to socket");
	}
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(55555);
	serveraddr.sin_addr.s_addr= 0; //inet_addr(argv[1]);
	if(bind(tcpfd,(struct sockaddr*)&serveraddr,len)<0)
	{
		perror("fail to bind");
	}
	if(listen(tcpfd,10)<0)
	{
		perror("fail to listen");
	}
	return tcpfd;
	
}

void * recvMsg(void *arg){
	
	int rlen;
	int clientFd;
	MSGA msga;
	clientFd = *(int *)arg;
	
	while(1){// 处理客户端数据死循环，只要客户端不断开，就一直接受数据
		memset(&msga,0,sizeof(msga));
		rlen = read(clientFd,&msga,LEN_SMG);
		if(rlen<=0)
		{//客户端断开，关闭客户端连接clientFd 返回到外层while循环
			close(clientFd);
			break;
		}
		switch(msga.type)
		{
        case R:
            project_register(clientFd,&msga,db);
            break;
        case L:
            project_login(clientFd,&msga,db);
            break;
        case Q:
            project_query(clientFd,&msga,db);
            break;
	case A:
            project_add(clientFd,&msga,db);
            break;
        case D:
            project_delete(clientFd,&msga,db);
            break;
	case B:
            project_edit(clientFd,&msga,db);
            break;
        case E:
            exit(0);
		}
	}
	printf("thread exit\n");
	
}


void project_register(int clientfd,MSGA *msga,sqlite3 *db)//注册用户
{
	extern su;
	char sql[M]={0};
	char *errmsg;
	char **rep;
	int n_row;
	int n_column;
	int i = 0,j = 0,index;
	sprintf(sql,"select id from user");
	if(sqlite3_get_table(db,sql,&rep,&n_row,&n_column,&errmsg) == SQLITE_OK)			
	{
		index = n_column;
		for(i = 0;i < n_row;i++)
		{
//			printf("第%d条数据\n",i);
			for(j = 0;j<n_column;j++)
			{
//				printf("字段名：%s---字段值：%s//",rep[j],rep[index]);
//					printf("%d,%d\n",j,index);
				sscanf(rep[index],"%d",&su);//su = (*(rep[index]));
				++index;
			}			
//			printf("%d\n",su);
			su = su+1;
		}		
	}
	
	
	msga->id = su;
	
	if(0 == su)
	{
		strcpy(msga->idter,"ssuper");
	}
	else
	{	
		strcpy(msga->idter,"normal");
	}
//	su++;
	sprintf(sql,"insert into user values('%d','%s','%s','%s','%s','%s',NULL,NULL)",msga->id,msga->name,msga->age,msga->addr,msga->password,msga->idter);
	printf("insert ok\n");
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msga->infor,"Fail");//注册失败返回给客户端
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		strcpy(msga->infor,"OK");//注册成功，返回给客户端
		send(clientfd,msga,LEN_SMG,0);
		return;
	}

}
void project_login(int clientfd,MSGA *msga,sqlite3 *db)//登录
{
	char sql[M]={0};
	char *errmsg;
	char **rep;
	int n_row;
	int n_column;
	int i = 0,j = 0,index;
	sprintf(sql,"select * from user where name='%s' and password='%s'",msga->name,msga->password);	
	if(sqlite3_get_table(db,sql,&rep,&n_row,&n_column,&errmsg)!=SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msga->infor,"Fail");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		if(n_row==0)//查不到
		{

			strcpy(msga->infor,"Fail");
			send(clientfd,msga,LEN_SMG,0);
			return;
		}
		else  //只要行数大于0，无需打印，直接返回成功
		{
			
			strcpy(msga->infor,"OK");
/*			index = n_column;
			for(i = 0;i < n_row;i++)
			{
				printf("第%d条数据\n",i+1);
				for(j = 0;j<n_column;j++)
				{
					printf("字段名：%s---字段值：%s//",rep[j],rep[index]);
					printf("%d,%d\n",j,index);
					++index;
				}
				printf("\n");
			}*/	
			strcpy(msga->idter,rep[13]);
			send(clientfd,msga,LEN_SMG,0);
			
			return;
		}
	}
	
}
void project_edit(int clientfd,MSGA *msga,sqlite3 *db)//修改信息
{
	char sql[M]={0};
	char *errmsg;
	sprintf(sql,"update user set '%s' ='%s' where id = '%d'",msga->infor,msga->age,msga->id);
   	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msga->infor,"Fail");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		strcpy(msga->infor,"OK");//修改成功，返回给客户端
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
}
void project_query(int clientfd,MSGA *msga,sqlite3 *db)//查看用户
{
	char sql[M]={0};
	char *errmsg;
	char **rep;
	int n_row;
	int n_column;
	int i = 0;
	if(strcmp(msga->name,"chazhaoid")==0)
	{
		sprintf(sql,"select * from user where id = %d ",msga->id);//数据库查询语句
	}	
	else
	{
		sprintf(sql,"select * from user where name = '%s' ",msga->name);//数据库查询语句
	}
	printf("%s\n",sql);
	if(sqlite3_get_table(db,sql,&rep,&n_row,&n_column,&errmsg)!=SQLITE_OK)
	{
		printf("%s\n",errmsg);
		printf("sqlite3 get table  success\n");
		strcpy(msga->infor,"Fail");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		if(n_row==0)//查不到
		{

			strcpy(msga->infor,"Fail");
			send(clientfd,msga,LEN_SMG,0);
			return;
		}
		else  //只要行数大于0，无需打印，直接返回成功
		{
			
			msga->id = *rep[8];
			strcpy(msga->name,rep[9]);
			strcpy(msga->age,rep[10]);
			strcpy(msga->addr,rep[11]);
			strcpy(msga->idter,rep[13]);
			//printf("id：%s---age：%s\n",rep[8],rep[10]);
			send(clientfd,msga,LEN_SMG,0);
			sqlite3_free_table(rep);
			return;
		}
	}	
}
void project_add(int clientfd,MSGA * msga,sqlite3 *db)//增加信息
{
	char sql[M]={0};
	char *errmsg;
	sprintf(sql,"insert into user values('%d','%s','%s','%s','%s','%s',NULL,NULL)",msga->id,msga->name,msga->age,msga->addr,msga->password,msga->idter);
    if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msga->infor,"Fail");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		strcpy(msga->infor,"OK");//注册成功，返回给客户端
		printf("insert success\n");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
}
void project_delete(int clientfd,MSGA *msga,sqlite3 *db)
{
	char sql[M]={0};
	char *errmsg;
	sprintf(sql,"delete from user where id='%d'",msga->id);
   	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msga->infor,"Fail");
		send(clientfd,msga,LEN_SMG,0);
		return;
	}
	else
	{
		strcpy(msga->infor,"OK");//注册成功，返回给客户端
		send(clientfd,msga,LEN_SMG,0);
		return;
	}	
}
